<?php
namespace Badmins\Controller;
class IndexController extends CkadminController {
    public function index(){
        $this->display();
    }
    public function save() {
    	$a['statusCode'] = 200;
    	$a['message'] = "";
    	echo json_encode($a);
    }
    public function errorer() {
    	$a['statusCode'] = 300;
    	$a['message'] = "失败";
    	echo json_encode($a);
    }
    public function timeout() {
    	$a['statusCode'] = 301;
    	$a['message'] = "登录超时,请重新登录";
    	echo json_encode($a);
    }
    public function upload(){
    	$basc = F('web_web');
    	$max_upload = $basc['upload_max']?$basc['upload_max']:'3145728';
		$exts = array('jpg','gif','png','jpeg');
		$msg = I('get.msg');
		$type = (int)I('get.type');
		if($type == 1){
			//css
			$rootpath = './Public/uploads/css/';
			$truepath = '/Public/uploads/css/';
		}else if($type == 2){
			//js
			$rootpath = './Public/uploads/js/';
			$truepath = '/Public/uploads/js/';
		}elseif ($type == 4){
			//语音
			$exts = array('mp3','wma','wav','amr');
			$rootpath = './Public/uploads/voice/';
			$truepath = '/Public/uploads/voice/';
		}elseif ($type == 5){
			//视频
			$exts = array('mp4','avi','3gp','rmvb');
			$rootpath = './Public/uploads/video/';
			$truepath = '/Public/uploads/video/';
		}else{
			$rootpath = './Public/uploads/photo/';
			$truepath = '/Public/uploads/photo/';
		}
		$upload = new \Think\Upload();
		$upload->maxSize = C('UPLOAD_MAX_SIZE');
		$upload->rootPath = $rootpath;
		$upload->saveName = array('uniqid','');
		$upload->exts     = $exts;
		$upload->autoSub  = true;
		$upload->subName  = array('date','Ymd');
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			showmsg(300,$upload->getError());exit();
		}else{			// 上传成功 获取上传文件信息
			$url = $rootpath.date("Ymd")."/".$info['file']['savename'];
			$a = getimagesize($url);
			$true_url = $truepath.date("Ymd")."/".$info['file']['savename'];
			// showmsg(200,$true_url);exit();
			if($type == 3){
				//微信图片添加到数据库
				$wx = F('wx_basc');
				$wx_id = \Org\Wx\wx::add_img(1, $true_url, $wx['wx_appid'], $wx['wx_appsecret']);
				$data['photo'] = $true_url;
				if($wx_id['media_id']){
					$data['wx_id'] = $wx_id;
				}
				M('wx_image')->add($data);
				
				
			}
			showmsg(200,$true_url,$msg);
		}
	}
    /**
     * 获取子分类
     */
	public function getmsg() {
		$v = I('get.v/d');
		if($v){
			$list = M('category')->where("status='0' and is_del='0' and parent_id='{$v}'")->field("id as value,name as label")->select();
		    if(!empty($list)){
		    	$res[0]['value'] = 0;
		    	$res[0]['label'] = "请选择";
		    	foreach ($list as $i=>$va){
		    		$res[$i+1]['value'] = $va['value'];
		    		$res[$i+1]['label'] = $va['label'];
		    	}
		    }else{
		    	$res = array();
		    }
		    
		}else{
			$res = array();
		}
		echo json_encode($res);
	}
	/**
	 * 获取区域
	 */
	public function getarea(){
		$v = I('get.v/d');
		if($v){
			$db = M('city');
			$list = $db->where("parent_id='{$v}' and status='0'")->field("id as value,name as label")->select();
			if(!empty($list)){
				$res[0]['value'] = 0;
				$res[0]['label'] = "请选择";
				foreach ($list as $i=>$va){
					$res[$i+1]['value'] = $va['value'];
					$res[$i+1]['label'] = $va['label'];
				}
			}else{
				$res = array();
			}
		}else{
			$res = array();
		}
		echo json_encode($res);
	}
	/**
	 * 获取分类
	 */
	public function getclass(){
		$v = I('get.v/d');
		if($v){
			$db = M('category');
			$list = $db->where("commend_id='{$v}' and status='0' and is_del='0'")->field("id,name,parent_id")->select();
			if(!empty($list)){
				$res[0]['value'] = 0;
				$res[0]['label'] = "请选择";
				$lists = \Org\Oa\common::unlimitedForLevel($list,'&nbsp;&nbsp;&nbsp;&nbsp;');
				foreach ($list as $i=>$va){
					$res[$i+1]['value'] = $va['id'];
					$res[$i+1]['label'] = $va['name'];
				}
			}else{
				$res = array();
			}
		}else{
			$res = array();
		}
		echo json_encode($res);
	}
	public function coupon() {
		$a['statusCode'] = 200;
		$a['message'] = "dasd1e1,dqw2131,dad12ffds,hjty4,343vds";
		echo json_encode($a);
	}
	
}